package testing.gui;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import jsudoku.graphics.JPlaySudoku;
import junit.framework.AssertionFailedError;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.uispec4j.AbstractUIComponent;
import org.uispec4j.Key;
import org.uispec4j.MenuBar;
import org.uispec4j.MenuItem;
import org.uispec4j.Panel;
import org.uispec4j.TextBox;
import org.uispec4j.Trigger;
import org.uispec4j.UIComponent;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.finder.ComponentMatcher;
import org.uispec4j.interception.BasicHandler;
import org.uispec4j.interception.MainClassAdapter;
import org.uispec4j.interception.WindowHandler;
import org.uispec4j.interception.WindowInterceptor;
import org.uispec4j.interception.handlers.InterceptionHandler;
import org.uispec4j.Mouse;

import junit.framework.Assert;

import org.uispec4j.assertion.Assertion;

import testing.utility.Matrice;

import com.sun.net.ssl.internal.www.protocol.https.Handler;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;

import java.awt.Component;

public class testMenuBar extends UISpecTestCase{
	
	private Panel main;
	private Window menuprova;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	public void setUp() throws Exception {
		setAdapter(new MainClassAdapter(JPlaySudoku.class, new String[0]));
        main = getMainWindow();
        menuprova = getMainWindow();
	}

	@After
	public void tearDown() throws Exception {
	}

	
	//Test ? -> Help
		@Test
		public void test1() {
			
			//Controllo che si apre la finestra "Help - JSudoku"
			
			MenuItem menu=menuprova.getMenuBar().getMenu("?");
			menu.click();
			
			MenuItem menu2=menu.getSubMenu("Help");
			 WindowInterceptor.init(menu2.triggerClick())
			  .process(new WindowHandler() {
			    public Trigger process(Window w) {	
			      assertTrue(w.getTitle().equals("Help - JSudoku"));
			      w.typeKey(Key.alt(Key.F4));
			      return Trigger.DO_NOTHING;
			    }
			  }).run();
			
		}
		
		//Test ? -> Informazioni su JSudoku
		@Test
		public void test2() {

			MenuItem menu=menuprova.getMenuBar().getMenu("?");
			//Si puo anche implementare questa riga di codice risparmiando riga 31 e 45
			//MenuItem menu=((Window)main).getMenuBar().getMenu("File");
			menu.click();
			//MenuItem menu=main.getMenuBar().getMenu("File");
			MenuItem menu2=menu.getSubMenu("Informazioni su JSudoku");
			 WindowInterceptor.init(menu2.triggerClick())
			  .process(new WindowHandler() {
			    public Trigger process(Window w) {
			      assertTrue(w.containsLabel("Creato da Paolo Ricciuti"));
			      w.typeKey(Key.alt(Key.F4));
			      return Trigger.DO_NOTHING;
			    }
			  }).run();
		
						
		}
		
		//Test File->Esci
		@Test
		public void test3() {
		
			/*
			MenuBar menuBar = getMainWindow().getMenuBar();
			MenuItem fileMenu = menuBar.getMenu("File");
			MenuItem exit = fileMenu.getSubMenu("Esci");
			exit.click();
			
			//System.out.println(menuBar.getDescriptionTypeName());
		
			 try 
		        {
				// System.exit(42);
				 getMainWindow().typeKey(Key.ESCAPE);
		        } catch (Exception e) 
		        {
		          e.printStackTrace();
		        }
			//getMainWindow().typeKey(Key.ESCAPE);
		//	System.out.println(getMainWindow().getDescription());
			//getMainWindow().getMenuBar().getMenu("File").getSubMenu("Esci").click();
			
			System.out.println(main.getDescription());
			*/
	
			 
			//assertFalse(getMainWindow().toString().startsWith("org.uispec4j.Window"));
						
		}
		
		//Test File->Salva
		@Test
		public void test4() {
			
			//Controllo che si apre la finestra "Salva"
			
			MenuItem menu=menuprova.getMenuBar().getMenu("File");
			menu.click();
			
			MenuItem menu2=menu.getSubMenu("Salva");
			 WindowInterceptor.init(menu2.triggerClick())
			  .process(new WindowHandler() {
			    public Trigger process(Window w) {	
			      assertTrue(w.getTitle().equals("Salva"));
			      w.typeKey(Key.alt(Key.F4));
			      return Trigger.DO_NOTHING;
			    }
			  }).run();
							
		}
		
		//Test File->Apri
				@Test
				public void test5() {
					
					//Controllo che si apre la finestra "Salva"
					
					MenuItem menu=menuprova.getMenuBar().getMenu("File");
					menu.click();
					
					MenuItem menu2=menu.getSubMenu("Apri");
					 WindowInterceptor.init(menu2.triggerClick())
					  .process(new WindowHandler() {
					    public Trigger process(Window w) {	
					      assertTrue(w.getTitle().equals("Apri"));
					      w.typeKey(Key.alt(Key.F4));
					      return Trigger.DO_NOTHING;
					    }
					  }).run();
									
				}

				//Test MOUSE
				@Test
				public void test6() {
					
					main.getDescription();
					int [][] m = Matrice.getMatriceInt(Matrice.getMatrice(main));
					//Matrice.stampaMatrice(m);
		
					UIComponent[] principal = main.getUIComponents(ComponentMatcher.ALL);
					
					principal[0].getAwtComponent();
					
					Component c= principal[1].getAwtComponent();

					
					Mouse.click(principal[1]);
					Mouse.doubleClick(principal[1]);
			
					//main.getUIComponents(uiComponentClass);
					main.pressKey(Key.NUM1);
					principal[0].pressKey(Key.NUM1);
					principal[1].pressKey(Key.NUM1);
					
					
					main.getDescription();
					int [][] m1 = Matrice.getMatriceInt(Matrice.getMatrice(main));
					//Matrice.stampaMatrice(m1);
					
					//boolean uguali = Matrice.confrontaMatrici(m, m1);
					//if(uguali) System.out.println("SONO UGUALI !!!");
				
					
					
									
				}
				
}
